import Config from './Config.js';
import Block from './Block.js';
import Direction from './Direction.js';
import Rocket from './Rocket.js';
import Area from './Area.js';
export default class Tank extends Block {
    constructor(x = 0, y = 0, width = 50, height = 50, imgSrc = Config.ImageAssets.TankImgs.top) {
        super(x, y, width, height, imgSrc);
        this.speed = 250;
        this.direction = Direction.TOP;
        this.life = 20;
        this.attack = 500;
        this.attackSpeed = 200; // 子弹的发射间隔 - 200ms
        this.isWeaponReady = true;
    }
    move(collisionArr) {
        // 移动速度 
        let step = this.speed / 1000 * Config.GameUpdateTick;
        console.log(step);
        // 计算下个位置是否通行
        let nextPos = {
            x: this.x,
            y: this.y
        }
        switch (this.direction) {
            case Direction.TOP:
                nextPos.y -= step;
                break;
            case Direction.RIGHT:
                nextPos.x += step;
                break;
            case Direction.BOTTOM:
                nextPos.y += step;
                break;
            case Direction.LEFT:
                nextPos.x -= step;
                break;
        }
        //边界判断　
        nextPos.x = nextPos.x < 0 ? 0 : nextPos.x;
        nextPos.y = nextPos.y < 0 ? 0 : nextPos.y;
        let maxX = Config.MapWidth - Config.BlockSize;
        let maxY = Config.MapHeight - Config.BlockSize;
        nextPos.x = nextPos.x > maxX ? maxX : nextPos.x;
        nextPos.y = nextPos.y > maxY ? maxY : nextPos.y;
        // 
        let canForward = true;
        for (let i = 0; i < collisionArr.length; i++) {
            let area = new Area(nextPos.x, nextPos.y, this.width, this.height);
            if (collisionArr[i].isCollision(area)) {
                canForward = false;
                break;
            }
        }
        if (canForward) {
            this.x = nextPos.x;
            this.y = nextPos.y;
        }
    }
    shoot(rocketImg) {
        this.isWeaponReady = false;
        setTimeout((() => {
            this.isWeaponReady = true;
        }).bind(this), this.attackSpeed);

        let rx = this.x + this.width / 2 - 10;
        let ry = this.y + this.height / 2 - 10;
        let rocket = new Rocket(rx, ry);
        rocketImg && (rocket.img.src = rocketImg);
        rocket.direction = this.direction;
        rocket.damage = this.attack;
        return rocket;
    }
}